package org.l.mybatis.demo;

import java.util.ArrayList;
import java.util.List;

import org.l.mybatis.demo.article.Article;
import org.l.mybatis.demo.article.ArticleMapper;
import org.l.mybatis.demo.user.User;
import org.l.mybatis.demo.user.UserMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
        user(context);
        article(context);
    }

    private static void user(ApplicationContext context) {
        System.out.println("user ======");
        UserMapper mapper = context.getBean(UserMapper.class);
        User user = mapper.getUser(new User(1));
        System.out.println(user);

        List<User> list = new ArrayList<>();
        user = new User();
        user.setName("bt1");
        list.add(user);
        user = new User();
        user.setName("bt2");
        list.add(user);
        mapper.insertBatch(list);
        list.forEach(e -> System.out.println(e.getName() + " id=" + e.getId()));
    }

    private static void article(ApplicationContext context) {
        System.out.println("article ======");
        Article article = new Article(1);
        ArticleMapper mapper = context.getBean(ArticleMapper.class);
        Article rs1 = mapper.selectOne(new Article(1));
        System.out.println(rs1);

        article = new Article();
        article.setTitle("aaaa");
        List<Article> list = mapper.selectList(article, "id desc");
        System.out.println(list);

        list = mapper.selectList(article, null);
        System.out.println(list);

        article = new Article(1);
        article.setTitle("Hello" + System.currentTimeMillis());
        mapper.insertOne(article);
        System.out.println("id=" + article.getId());

        list = new ArrayList<>();
        article = new Article();
        article.setTitle("batch1" + System.currentTimeMillis());
        list.add(article);
        article = new Article();
        article.setTitle("batch2" + System.currentTimeMillis());
        list.add(article);
        mapper.insertList(list);
        list.forEach(e -> System.out.println(e.getTitle() + " id=" + e.getId()));

        article = new Article(1);
        article.setTitle("bbb" + System.currentTimeMillis());
        int rs = mapper.updateOne(article);
        System.out.println(rs);

        article = new Article(1);
        mapper.deleteOneOrMore(article);
    }
}
